package com.jzh.cmhSystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import com.jzh.cmhSystem.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
@Api(tags = "验证码相关", value = "这是描述")
//@Tag(name = "验证码相关", description = "这是描述")
@RestController
@RequestMapping("/api/captcha")
//@Slf4j
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/get")
    @Operation(summary = "获取验证码")
    public Result get() {
        System.out.println("get captcha");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 10);
        //得到验证码
        String code = lineCaptcha.getCode();
        //有效时长
        redisTemplate.expire("code",30, TimeUnit.SECONDS);
        //redis存储验证码
        redisTemplate.opsForValue().set("code", code);
        //返回Base64编码的图片JSOW字苻串
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        //返回JSON格式ResponseResult.对象
        return Result.success(imageBase64Data);
    }

}
